package com.gexin.controller.weixin.subscribe;

import com.jfinal.plugin.redis.Redis;

/**
 * @author 订阅消息工具类
 * @Title: SubscribeMessageKit
 * @ProjectName maven-web-admin
 * @Description: TODO
 * @date 2019/12/3110:23
 */
public class SubscribeMessageKit {
	/*测试模板消息ID  ， 这个需要自行去微信小程序后台添加*/
	public static final String Subscribe_Test_Message = "xMRItqtQqLzZYQ903AjF82KPBOshO2zO5EuGyKDpmZc";

	/** 代理商 审核结果提醒 */
	public static final  String  Subscribe_Agent_Review_Message="h9Wbk4Xe5E6CW2aCV3RNwTy9evrzFPO1MNC_pH-JhWA";

	/** 代理商 审核结果提醒 */
	public static final  String  User_Order_Push_Agent="ftz4XPP7PVvQGdKI_S0BqFbA52nRTwxU0lK9poT4IY0";

	public static SubscribeMessageKit api = new SubscribeMessageKit();
	private static final String cache_key = "subscribe:wxapp:";

	/**
	 * 添加一个订阅消息计数器
	 *
	 * @param userId      用户ID
	 * @param subscribeId 订阅消息ID
	 */
	public void addSubscribe(Integer userId, String subscribeId) {
		Redis.use().incrBy(cache_key + userId + ":" + subscribeId, 1);
	}


	/**
	 * 判断这个用户是否有订阅消息
	 *
	 * @param userId      用户ID
	 * @param subscribeId 订阅消息的DI
	 * @return true 表示可以推送false 表示没有订阅消息
	 */
	public Boolean subSubscribe(Integer userId, String subscribeId) {
		String key = cache_key + userId + ":" + subscribeId;
		Long subscribe = Redis.use().getCounter(key);
		if (subscribe != null && subscribe > 0) {
			Redis.use().decrBy(key, 1);
			return true;
		}
		return false;
	}
}
